package user.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import user.vo.FeedBack;
import user.vo.GainPassTemplateRequest;
import user.vo.PassTemplate;

/**
 * RowKey生成器工具类
 */
@Slf4j
public class RowKeyGenUtil {

    /** 根据提供的 PassTemplate 对象生成 RowKey
     * PassTemplate {@link PassTemplate}
     */
    public static String genPassTemplateRowKey(PassTemplate passTemplate){

        String passInfo = String.valueOf(passTemplate.getId()) + "_" + passTemplate.getTitle();
        //生成rowKey
        String rowKey = DigestUtils.md5Hex(passInfo);
        log.info("GenPassTemplateRowKey:{},{}",passInfo,rowKey);

        return rowKey;
    }

    /**
     * 根据提供的领取优惠券请求生成RowKey,只可以在领取优惠券的时候使用
     * Pass RowKey = reversed(userId) + inverse(timestamp) + PassTemplate RowKey
     * @param request {@link GainPassTemplateRequest}
     * @return String RowKey
     */
    public static String genPassRowKey(GainPassTemplateRequest request){

        return new StringBuilder(String.valueOf(request.getUserId())).reverse().toString()
                   + (Long.MAX_VALUE - System.currentTimeMillis())
                   + genPassTemplateRowKey(request.getPassTemplate());
    }
    /**
     * 根据 FeedBack 构造 rowKey
     * @param feedBack {@link FeedBack}
     * @return String RowKey
     */
    public static String genFeedBackRowKey(FeedBack feedBack){

        return new StringBuilder(String.valueOf(feedBack.getUserId())).reverse().toString() +
                (Long.MAX_VALUE - System.currentTimeMillis());
    }
}
